# -*- coding: utf-8 -*-
import baostock as bs
import pandas as pd
import datetime


def get_fund_data(begin, end, code):
    rs = bs.query_history_k_data_plus(code,
    "date,code,open,high,low,close,preclose,volume,amount,pctChg",
    start_date=begin, end_date=end, frequency="d")

    data_list = []
    data = rs.get_data()
    num = len(data)-1
    if num > 0:
        data_list.append(data["date"][num])
        data_list.append(data["code"][num])
        data_list.append(data["close"][num])
        data_list.append(data["pctChg"][num])
    return data_list


if __name__ == '__main__':
    # 登陆系统
    lg = bs.login()
  
    now  = datetime.datetime.now()
    end = now.strftime('%Y-%m-%d')
    begin = (now + datetime.timedelta(days=-7)).strftime('%Y-%m-%d')
    #上证50
    sh50 = get_fund_data(begin, end, 'sh.000016') 
    sh50.append("上证50")
    #沪深300
    sh300 = get_fund_data(begin, end, 'sh.000300') 
    sh300.append("沪深300")
    #中证红利
    hongli = get_fund_data(begin, end, 'sh.000922') 
    hongli.append("中证红利")
    #中证500
    sh500 = get_fund_data(begin, end, 'sh.000905') 
    sh500.append("中证500")
    #创业板
    chuanye = get_fund_data(begin, end, 'sz.399006') 
    chuanye.append("创业板")
     
    print(sh50)
    print(sh300)
    print(hongli)
    print(sh500)
    print(chuanye)

    base_data = [("上证50", 2750),  ("沪深300", 3660),("中证红利", 4350),("中证500", 4950),("创业板", 1450) ]
    print(base_data)
  
    # 登出系统
    bs.logout()